package com.fly.estore.controller;

import com.fly.estore.domain.*;
import com.fly.estore.framework.common.AjaxResult;
import com.fly.estore.framework.session.AccountSession;
import com.fly.estore.framework.session.support.SessionInfo;
import com.fly.estore.framework.util.CommonUtil;
import com.fly.estore.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/16.
 */
@Controller
@RequestMapping("/product")
public class ProductController
{
    @Autowired
    ProductService productService;

    @RequestMapping("/infoList")
    public String productInfoList(
            ModelMap model
    ){
        List<ProductBrand> brandList = productService.getBrandList();
        model.addAttribute("brandList", brandList);
        List<ProductCatalog> catalogList = productService.getCatalogList();
        model.addAttribute("catalogList", catalogList);

        List<ProductInfo> productList = productService.queryProductList("", "", "", "");
        model.addAttribute("productList", productList);
        return "/product/info/list";
    }

    @RequestMapping("/infoDetail")
    public String productInfoDetail(
            @RequestParam(value = "productId", required = false, defaultValue = "")String productId,
            ModelMap model
    ){
        ProductInfo productInfo = productService.getProductById(productId);
        List<ProductSKU> skuList = productService.getSKUListByProductId(productId);
        productInfo.setSkuList(skuList);
        model.addAttribute("product", productInfo);
        List<ProductCatalog> catalogList = productService.getCatalogList();
        model.addAttribute("catalogList", catalogList);
        List<ProductBrand> brandList = productService.getBrandList();
        model.addAttribute("brandList", brandList);
        return "/product/info/detail";
    }

    @RequestMapping("/saveProduct")
    @ResponseBody
    public AjaxResult saveProduct(
            @RequestParam(value = "productId", required = false, defaultValue = "")String productId,
            @RequestParam(value = "productName", required = false, defaultValue = "")String productName,
            @RequestParam(value = "productCode", required = false, defaultValue = "")String productCode,
            @RequestParam(value = "catalogId", required = false, defaultValue = "")String catalogId,
            @RequestParam(value = "brandId", required = false, defaultValue = "")String brandId,
            @RequestParam(value = "productIntro", required = false, defaultValue = "")String productIntro,
            @RequestParam(value = "description", required = false, defaultValue = "")String description,
            @SessionInfo AccountSession accountSession
    ){
        ProductInfo product = new ProductInfo();
        product.setProductId(productId);
        product.setProductName(productName);
        product.setProductCode(productCode);
        ProductCatalog catalog = new ProductCatalog();
        catalog.setProductCatalogId(catalogId);
        product.setCatalog(catalog);
        ProductBrand brand = new ProductBrand();
        brand.setProductBrandId(brandId);
        product.setBrand(brand);
        product.setProductIntro(productIntro);
        product.setDescription(description);
        product.setCreateId(accountSession.getAccountId());
        product.setCreateTime(CommonUtil.currentTime());
        product.setLastUpdateId(accountSession.getAccountId());
        product.setLastUpdateTime(CommonUtil.currentTime());
        productId = StringUtils.isNotBlank(productId) ? productId : CommonUtil.genPK();
        int update = productService.saveProduct(product, productId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        result.setData(productId);
        return result;
    }

    @RequestMapping("/deleteProduct")
    @ResponseBody
    public AjaxResult deleteProduct(
            @RequestParam("productId")String productId
    ){
        int update = productService.deleteProductById(productId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/getProductListByCatalogId")
    @ResponseBody
    public AjaxResult getProductListByCatalogId(
            @RequestParam("catalogId")String catalogId
    ){
        List<ProductInfo> productList = productService.queryProductList("", "", catalogId, "");
        AjaxResult result = new AjaxResult();
        result.setStatus(AjaxResult.STATUS_SUCCESS);
        result.setData(productList);
        return result;
    }

    //------------------------商品品牌-------------------------------------//
    @RequestMapping("/brandDetail")
    public String brandDetail(
            @RequestParam(value = "brandId", required = false, defaultValue = "")String productBrandId,
            ModelMap model
    ){
        ProductBrand brand = new ProductBrand();
        if (StringUtils.isNotBlank(productBrandId)){
            brand = productService.getBrandById(productBrandId);
        }
        model.addAttribute("brand", brand);
        return "/product/brand/detail";
    }

    @RequestMapping("/saveBrand")
    @ResponseBody
    public AjaxResult saveBrand(
            @RequestParam(value = "brandId", required = false, defaultValue = "")String productBrandId,
            @RequestParam(value = "brandName", required = false, defaultValue = "")String brandName,
            @RequestParam(value = "brandIntro", required = false, defaultValue = "")String brandIntro,
            @SessionInfo AccountSession accountSession
            ){
        ProductBrand brand = new ProductBrand();
        brand.setProductBrandId(productBrandId);
        brand.setBrandName(brandName);
        brand.setBrandIntro(brandIntro);
        brand.setCreateId(accountSession.getAccountId());
        brand.setCreateTime(CommonUtil.currentTime());
        brand.setLastUpdateId(accountSession.getAccountId());
        brand.setLastUpdateTime(CommonUtil.currentTime());
        int update = productService.saveProductBrand(brand);

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return ajaxResult;
    }

    @RequestMapping("/deleteBrand")
    @ResponseBody
    public AjaxResult deleteBrand(
            @RequestParam("brandId")String productBrandId
    ){
        int update = productService.deleteBrandById(productBrandId);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return ajaxResult;
    }

    //-----------------------------------商品规格-------------------------------------//
    @RequestMapping("/specList")
    public String productSpecList(
            ModelMap model
    ){
        List<ProductSpec> specList = productService.getSpecList();
        model.addAttribute("specList", specList);
        return "/product/spec/list";
    }

    @RequestMapping("/specDetail")
    public String productSpecDetail(
            @RequestParam(value = "specId", required = false, defaultValue = "")String productSpecId,
            ModelMap model
    ){
        ProductSpec spec = productService.getSpecById(productSpecId);
        model.addAttribute("spec", spec);
        return "/product/spec/detail";
    }

    @RequestMapping("/saveSpec")
    @ResponseBody
    public AjaxResult saveProductSpec(
            @RequestParam(value = "specId", required = false, defaultValue = "")String productSpecId,
            @RequestParam(value = "specName", required = false, defaultValue = "")String specName,
            @RequestParam(value = "specValue", required = false, defaultValue = "")String specValue,
            @SessionInfo AccountSession accountSession
    ){
        ProductSpec spec = new ProductSpec();
        spec.setProductSpecId(productSpecId);
        spec.setSpecName(specName);
        spec.setSpecValues(specValue);
        spec.setCreateId(accountSession.getAccountId());
        spec.setCreateTime(CommonUtil.currentTime());
        spec.setLastUpdateId(accountSession.getAccountId());
        spec.setLastUpdateTime(CommonUtil.currentTime());
        int update = productService.saveProductSpec(spec);

        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/deleteSpec")
    @ResponseBody
    public AjaxResult deleteProductSpec(
            @RequestParam("specId")String productSpecId
    ){
        int update = productService.deleteSpecById(productSpecId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }
    //-----------------------------------end 商品规格-----------------------------------------------//

    //-----------------------------------begin 商品类型 --------------------------------------------//
    @RequestMapping("/catalogDetail")
    public String productCatalogDetail(
            @RequestParam(value = "catalogId", required = false, defaultValue = "")String productCatalogId,
            ModelMap model
    ){
        ProductCatalog catalog = productService.getCatalogById(productCatalogId);
        if (null != catalog){
            catalog.setCatalogSpecList(productService.getCatalogSpecListByCatalogId(catalog.getProductCatalogId()));
        }
        model.addAttribute("catalog", catalog);
        List<ProductSpec> specList = productService.getSpecList();
        model.addAttribute("specList", specList);
        return "/product/catalog/detail";
    }

    @RequestMapping("/saveCatalog")
    @ResponseBody
    public AjaxResult saveProductCatalog(
            @RequestParam("catalogId") String productCatalogId,
            @RequestParam("catalogName")String catalogName,
            @RequestParam("spec")String specs,
            @RequestParam("orderNum")int orderNum,
            @RequestParam("discount")String discount,
            @SessionInfo AccountSession accountSession
    ){
        ProductCatalog catalog = new ProductCatalog();
        catalog.setProductCatalogId(productCatalogId);
        catalog.setCatalogName(catalogName);
        catalog.setOrderNum(orderNum);
        catalog.setDiscount(discount);
        catalog.setCreateId(accountSession.getAccountId());
        catalog.setCreateTime(CommonUtil.currentTime());
        catalog.setLastUpdateId(accountSession.getAccountId());
        catalog.setLastUpdateTime(CommonUtil.currentTime());

        String[] specArray = specs.split(",");
        List<ProductCatalogSpec> catalogSpecList = new ArrayList<ProductCatalogSpec>();
        for(String specId : specArray){
            ProductCatalogSpec catalogSpec = new ProductCatalogSpec();
            ProductSpec spec = new ProductSpec();
            spec.setProductSpecId(specId);
            catalogSpec.setProductSpec(spec);
            catalogSpecList.add(catalogSpec);
        }
        catalog.setCatalogSpecList(catalogSpecList);

        int update = productService.saveProductCatalog(catalog);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/deleteCatalog")
    @ResponseBody
    public AjaxResult deleteCatalog(
            @RequestParam("catalogId")String productCatalogId
    ){
        int update = productService.deleteCatalogById(productCatalogId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/getCatalogList")
    @ResponseBody
    public AjaxResult getCatalogList(

    ){
        List<ProductCatalog> catalogList = productService.getCatalogList();
        AjaxResult result = new AjaxResult();
        result.setStatus(AjaxResult.STATUS_SUCCESS);
        result.setData(catalogList);
        return result;
    }

    //---------------------------------------商品规格属性----------------------------------------------//
    @RequestMapping("/skuDetail")
    public String productSKUDetail(
            @RequestParam(value = "productSkuId", required = false, defaultValue = "")String productSkuId,
            @RequestParam(value = "productId", required = false, defaultValue = "")String productId,
            ModelMap model
    ){
        ProductSKU productSKU = new ProductSKU();
        if (StringUtils.isNotBlank(productSkuId)){
            productSKU = productService.getSKUById(productSkuId);
        }
        List<ProductCatalogSpec> specList = productService.getCatalogSpecListByCatalogId(productService.getProductById(productId).getCatalog().getProductCatalogId());
        model.addAttribute("sku", productSKU);
        model.addAttribute("productId", productId);
        model.addAttribute("specList", specList);
        return "/product/sku/detail";
    }

    @RequestMapping("/saveSKU")
    @ResponseBody
    public AjaxResult saveProductSKU(
            @RequestParam(value = "productSkuId", required = false, defaultValue = "")String productSkuId,
            @RequestParam(value = "productId", required = false, defaultValue = "")String productId,
            @RequestParam(value = "skuName",required = false, defaultValue = "")String skuName,
            @RequestParam(value = "skuCode", required = false, defaultValue = "")String skuCode,
            @RequestParam(value = "skuSpecs", required = false, defaultValue = "")String skuSpecs,
            @RequestParam(value = "sellPrice", required = false, defaultValue = "0")float sellPrice,
            @RequestParam(value = "marketPrice", required = false, defaultValue = "0")float marketPrice,
            @RequestParam(value = "purchasePrice", required = false, defaultValue = "0")float purchasePrice,
            @SessionInfo AccountSession accountSession
    ){
        ProductSKU productSKU = new ProductSKU();
        ProductInfo product = new ProductInfo();
        productSKU.setProductSkuId(productSkuId);
        product.setProductId(productId);
        productSKU.setProductInfo(product);
        productSKU.setSkuCode(skuCode);
        productSKU.setSkuName(skuName);
        productSKU.setSkuSpecs(skuSpecs);
        productSKU.setSellPrice(sellPrice);
        productSKU.setMarketPrice(marketPrice);
        productSKU.setPurchasePrice(purchasePrice);
        int update = productService.saveProductSKU(productSKU);

        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/deleteSKU")
    @ResponseBody
    public AjaxResult deleteProductSKU(
            @RequestParam("productSkuId")String productSkuId
    ){
        int update = productService.deleteProductSKUById(productSkuId);

        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/getSkuListByProductId")
    @ResponseBody
    public AjaxResult getSkuListByProductId(
            @RequestParam("productId")String productId
    ){
        List<ProductSKU> skuList = productService.getSKUListByProductId(productId);

        AjaxResult result = new AjaxResult();
        result.setStatus(null != skuList ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        result.setData(skuList);
        return result;
    }
}
